//
//  UIButton+Extension.swift
//  Weibo
//
//  Created by 张小东 on 16/1/17.
//  Copyright © 2016年 Mr.Zhang. All rights reserved.
//

import UIKit

extension UIButton {
    convenience init(imageName: String, backImageName:String) {
        self.init()
        //  设置btn的背景图片
        setBackgroundImage(UIImage(named: imageName), forState: .Normal)
        setBackgroundImage(UIImage(named: imageName + "_highlighted"), forState: .Highlighted)
        //设置按钮的图片
        setImage(UIImage(named: backImageName), forState: .Normal)
        setImage(UIImage(named: backImageName + "_highlighted"), forState: .Highlighted)
        // 设置自适应大小
        sizeToFit()
        
    }
    convenience init(title: String, fontSize : CGFloat, color: UIColor, backImageName: String?,imageName: String? = nil){
        self.init()
        //设置按钮的文字
        setTitle(title, forState: .Normal)
        titleLabel?.font = UIFont.systemFontOfSize(fontSize)
        setTitleColor(color, forState: .Normal)
        if backImageName != nil {
//            let image = UIImage(named: backImageName!)
//            let w = Int(image!.size.width * 0.5)
//            let h = Int(image!.size.height * 0.5)
//            setBackgroundImage(image!.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
             setBackgroundImage(UIImage(named: backImageName!), forState: .Normal)
        }
        if imageName != nil {
            setImage(UIImage(named: imageName!), forState: .Normal)
            
        }
        sizeToFit()
        //    setBackgroundImage(UIImage(named: backImageName), forState: .Normal)
    }
}